package main

import (
	"fmt"
	"math"
	"sort"
	"time"
)

//实现对圆形、三角形、长方形求面积。

type AreaInterface interface {
	Area() float64
	GetName() string
}

// 圆形
type Circle struct {
	radius float64
	name   string
}

func NewCircle(radius float64, name string) *Circle {
	return &Circle{radius: radius, name: name}
}
func (circle *Circle) Area() float64 {
	return math.Pi * circle.radius
}
func (circle *Circle) GetName() string {
	return circle.name
}

// 三角形
type Triangle struct {
	base  float64
	hight float64
	name  string
}

func NewTriangle(base float64, hight float64, name string) *Triangle {
	return &Triangle{base: base, hight: hight, name: name}
}
func (triangle *Triangle) Area() float64 {
	return 0.5 * triangle.base * triangle.hight
}
func (triangle *Triangle) GetName() string {
	return triangle.name
}

//长方形

type Rectangle struct {
	Length float64
	With   float64
	name   string
}

func NewRectangle(length float64, with float64, name string) *Rectangle {
	return &Rectangle{Length: length, With: with, name: name}
}

func (rectangle *Rectangle) Area() float64 {
	return rectangle.Length * rectangle.With
}
func (rectangle *Rectangle) GetName() string {
	return rectangle.name
}

/**
*利用第1题，构造3个以上图形，至少圆形、三角形、矩形各有一个
*对上题的图形按照面积降序排列
**/
type AreaSlice []AreaInterface

func (x AreaSlice) Len() int           { return len(x) }
func (x AreaSlice) Less(i, j int) bool { return x[i].Area() < x[j].Area() }
func (x AreaSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }

// Sort is a convenience method: x.Sort() calls Sort(x).
func (x AreaSlice) Sort() { sort.Sort(x) }
func (x AreaSlice) Format(f fmt.State, verb rune) {
	s := ""
	for _, value := range x {
		s += fmt.Sprintf("%s面积:%f\n", value.GetName(), value.Area())
	}
	fmt.Println(s)
}

// 分析斐波那契数列递推公式递归版本的问题，试着为该函数提供一个Cache，使其计算效率提升
func FibonacciSequence(n int) []int {
	numbers := make([]int, 0, n)
	c := make(map[int]int, n)
	for i := 0; i < n; i++ {
		numbers = append(numbers, fibonacci(i, c))
	}
	return numbers
}
func fibonacci(n int, c map[int]int) int {
	if n == 0 {
		return 1
	}
	if n == 1 {
		return 1
	}
	_, ok := c[n-1]
	if !ok {
		c[n-1] = fibonacci(n-1, c)

	}
	_, ok = c[n-2]
	if !ok {
		c[n-2] = fibonacci(n-2, c)

	}
	return c[n-1] + c[n-2]
}

// 8年前的今天上午9点30分
func convertoMilliseconds(year int, hour int, min int) int64 {
	location, _ := time.LoadLocation("Asia/Shanghai")
	time.Local = location
	t := time.Now().AddDate(-year, 0, 0)

	tthen := time.Date(t.Year(), t.Month(), t.Day(), hour, min, 0, 0, t.Location())
	m := tthen.UnixMilli()
	fmt.Printf("%v\n", tthen.Format("2006/01/02  15:04:05 -0700"))
	fmt.Printf("那天是：周%+v\n", int(tthen.Weekday()))
	diff := time.Until(tthen)
	fmt.Printf("已过%v周\n", int(diff.Abs().Hours()/24/7))
	fmt.Printf("已过%v天\n", int(diff.Abs().Hours()/24))
	return m
}

func main() {
	// 第1题和第2题

	// 第3题
	result := FibonacciSequence(10)
	fmt.Println(result)
	// 第4题
	t := time.Now()
	convertoMilliseconds(t.Year(), t.Hour(), t.Minute())
}

// 阿飞老师的批改
// 1. 没有最终的验证代码
// 2. 未定义出保存3个图形的数组;
// 3. 第4题实现逻辑有问题
